package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.commons.dbcp.BasicDataSource;

public class DBUtil {
	private static final String CLASSNAME = "oracle.jdbc.OracleDriver";
	private static final String URL = "jdbc:oracle:thin:@zha-ita089-w7:1521:orcl";
	private static final String USERNAME = "ziv";
	private static final String PSWD = "123456";
	private static BasicDataSource ds;
	
	static{
		ds = new BasicDataSource();
		ds.setDriverClassName(CLASSNAME);
		ds.setUrl(URL);
		ds.setUsername(USERNAME);
		ds.setPassword(PSWD);
		ds.setMaxActive(5);
	}
	
	
	public static Connection createConnectionWithDatasource(){
		Connection conn = null;
		try{
			conn= ds.getConnection();
		}catch(Exception e){
			e.printStackTrace();
		}
		return conn;
	}
	
	public static Connection createConnection (){
		Connection conn = null;
		try {
			Class.forName(CLASSNAME);
			conn=DriverManager.getConnection(URL,USERNAME,PSWD);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;
	}
	
	public static void close(Connection conn,PreparedStatement pst,ResultSet rs){
		try {
			if(rs!=null)rs.close();
			if(pst!=null)pst.close();
			if(conn!=null)conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
}
